package com.qf.homework.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler{
 private Object target;

    public MyProxy() {
    }

    public MyProxy(Object target) {
        this.target = target;
    }

    /**
     *
     * @param <T>
     * @return
     */
    public <T> T getProxy(){
        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法执行前的命令
        long start = System.currentTimeMillis();
        System.out.println(target+"\n执行方法:");
        System.out.println(method.getName());
        //方法执行
        Object invoke = method.invoke(target, args);
        //方法执行后的函数
        System.out.println(target+"\n执行方法:");
        System.out.println(method.getName());
        System.out.println("执行时间\n"+String.valueOf(System.currentTimeMillis()-start));
           return invoke;
    }
}
